package com.ysm.smfu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/14.
 */
public class PaysetMoneyDetailActivity extends BaseActivity {
    @BindView(R.id.act_payset_name)
    TextView actPaysetName;
    @BindView(R.id.act_payset_num)
    EditText actPaysetNum;
    @BindView(R.id.act_payset_beizhu)
    EditText actPaysetBeizhu;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    private String bussinessId;
    private String name;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_payset);
        ButterKnife.bind(this);
//        actPaysetNum.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//        actPaysetNum.setKeyListener(new DigitsKeyListener(false, true));
        UIUtils.setDeciNum(actPaysetNum, 2);
        getConfig();
    }

    private void getConfig() {
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        bussinessId = currShared.getString(SpKey.BUSSINESSID, "");
        if (TextUtils.isEmpty(bussinessId)) {
            ToastUtils.showToast("无法获取商户信息，请重新登录");
            return;
        }
        String shanghuName = currShared.getString(SpKey.SHANGHUNAME, "");
        titleBody.setText("消费订单金额设置");
        mainActivityTopbarImg.setVisibility(View.GONE);
        actPaysetName.setText(shanghuName);
    }

    @OnClick({R.id.next, R.id.back})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.next:
                String money = actPaysetNum.getText().toString().trim();
                if (TextUtils.isEmpty(money) || Double.parseDouble(money) < 0.01) {
                    ToastUtils.showToast("金额不能小于0.01元");
                    return;
                }
                String beizhu = actPaysetBeizhu.getText().toString().trim();
                if (TextUtils.isEmpty(beizhu))
                    beizhu = "无";
//                生成订单
                createDingDanByMoney(money, beizhu);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    /**
     * 根据商家设置生成订单
     */
    private void createDingDanByMoney(String totalPrice, final String remark) {
        if (!totalPrice.contains("."))
            totalPrice = totalPrice + ".00";
        else {
            String[] split = totalPrice.split("\\.");
            if (split != null && split[1].length() == 1)
                totalPrice = totalPrice + "0";
        }

        Intent intent = new Intent(PaysetMoneyDetailActivity.this, PaySetActivityAgain.class);
        intent.putExtra(SpKey.MONEY, totalPrice);
        intent.putExtra(SpKey.REMARK, remark);
        intent.putExtra(SpKey.Pay_BUSSINESSID, bussinessId);
        startActivity(intent);
    }
}
